/*** Eclipse Class Decompiler plugin, copyright (c) 2012 Chao Chen (cnfree2000@hotmail.com) ***/
package com.qihoo.gamecenter.sdk.common.i;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.channels.FileChannel;

public class g {
    public static void a(File paramFile1, File paramFile2) {
        a(paramFile1, paramFile2, true);
    }

    public static void a(File paramFile1, File paramFile2, boolean paramBoolean) {
        if (paramFile1 == null)
            throw new NullPointerException("Source must not be null");
        if (paramFile2 == null)
            throw new NullPointerException("Destination must not be null");
        if (!(paramFile1.exists()))
            throw new FileNotFoundException("Source '" + paramFile1 + "' does not exist");
        if (paramFile1.isDirectory())
            throw new IOException("Source '" + paramFile1 + "' exists but is a directory");
        if (paramFile1.getCanonicalPath().equals(paramFile2.getCanonicalPath()))
            throw new IOException("Source '" + paramFile1 + "' and destination '" + paramFile2 + "' are the same");
        if ((paramFile2.getParentFile() != null) && (!(paramFile2.getParentFile().exists()))
                && (!(paramFile2.getParentFile().mkdirs())))
            throw new IOException("Destination '" + paramFile2 + "' directory cannot be created");
        if ((paramFile2.exists()) && (!(paramFile2.canWrite())))
            throw new IOException("Destination '" + paramFile2 + "' exists but is read-only");
        b(paramFile1, paramFile2, paramBoolean);
    }

    private static void b(File paramFile1, File paramFile2, boolean paramBoolean) {
        if ((paramFile2.exists()) && (paramFile2.isDirectory()))
            throw new IOException("Destination '" + paramFile2 + "' exists but is a directory");
        FileInputStream localFileInputStream = null;
        FileOutputStream localFileOutputStream = null;
        FileChannel localFileChannel1 = null;
        FileChannel localFileChannel2 = null;
        try {
            localFileInputStream = new FileInputStream(paramFile1);
            localFileOutputStream = new FileOutputStream(paramFile2);
            localFileChannel1 = localFileInputStream.getChannel();
            localFileChannel2 = localFileOutputStream.getChannel();
            long l1 = localFileChannel1.size();
            long l2 = 0L;
            long l3 = 0L;
            while (l2 < l1) {
                l3 = (l1 - l2 > 52428800L) ? 52428800L : l1 - l2;
                l2 += localFileChannel2.transferFrom(localFileChannel1, l2, l3);
            }
        } finally {
            h.a(localFileChannel2);
            h.a(localFileOutputStream);
            h.a(localFileChannel1);
            h.a(localFileInputStream);
        }
        if (paramFile1.length() != paramFile2.length())
            throw new IOException("Failed to copy full contents from '" + paramFile1 + "' to '" + paramFile2 + "'");
        if (!(paramBoolean))
            return;
        paramFile2.setLastModified(paramFile1.lastModified());
    }

    public static FileOutputStream a(File paramFile) {
        if (paramFile.exists()) {
            if (paramFile.isDirectory())
                throw new IOException("File '" + paramFile + "' exists but is a directory");
            if (!(paramFile.canWrite()))
                throw new IOException("File '" + paramFile + "' cannot be written to");
        }
        else {
            File localFile = paramFile.getParentFile();
            if ((localFile != null) && (!(localFile.exists())) && (!(localFile.mkdirs())))
                throw new IOException("File '" + paramFile + "' could not be created");
        }
        return new FileOutputStream(paramFile);
    }

    public static void a(InputStream paramInputStream, File paramFile) {
        try {
            FileOutputStream localFileOutputStream = a(paramFile);
            try {
                h.a(paramInputStream, localFileOutputStream);
            } finally {
                h.a(localFileOutputStream);
            }
        } finally {
            h.a(paramInputStream);
        }
    }
}